using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using AppTableUtils;
using System;
using System.Text.RegularExpressions;


public class TranslatorManager : MonoBehaviour
{

    [Header("是否使用翻译")]
    public bool use_translate = true;

    [Header("当前语言")]
    public string language = "cn";
    string last_language;

    [Header("是否中文")]
    public bool is_cn = true;

    [Header("模板变量")]
    public Dictionary<string, object> template_variable_dc = new Dictionary<string, object>();

    // 是否符合`{{ xx }}`之类的模板匹配格式
    public bool ContainsTemplateSymbol(string input)
    {
        // 定义正则表达式模式
        string pattern = @"\{\{.*?\}\}";

        // 使用 Regex.IsMatch 方法检查字符串是否包含特殊符号
        return Regex.IsMatch(input, pattern);
    }
    
    // 切换使用语言
    public void btn_change_language()
    {
        is_cn = !is_cn;
    }

    public Dictionary<string, TranslateText> translate_comp_dc = new Dictionary<string, TranslateText>();

    public static TranslatorManager instance;

    void Awake()
    {
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    string default_from_language = "cn";

    // --- 将输入的`text`从`from_language`转换为`to_language`
    public string translate_input_text_string(string text, string from_language = null, string to_language = null)
    {
        if (from_language == null)
            from_language = default_from_language;

        if (to_language == null)
            to_language = language;

        if (from_language == to_language)
            return text;

        string select = $"{from_language}='{text}'";
        var row = AppTableUtils.TableUtils.get(translator_dt, select: select);
        BdUtils.assert(row, $" *** text[{text}]` + from_language[{from_language}]对应`row`为空?");

        BdUtils.assert(translator_dt.Columns.Contains(to_language), $"`translator_dt`没有列`to_language[{to_language}]`");
        var _text = row[to_language];
        BdUtils.assert(_text, $" *** `text[{text}]`对应`to_language[{to_language}]`为空?");

        string translated_text = BdUtils.conv_to_string(_text);
        return translated_text;
    }

    [Header("code无对应时是否报错")]
    public bool flag__raise_error_if_code_map_none = false;
    public string get_translated_text_by_code(string code)
    {
        var row = AppTableUtils.TableUtils.get(translator_dt, code);
        var is_error = BdUtils.assert(row, $" *** `code[{code}]`对应`row`为空?", raise_error: flag__raise_error_if_code_map_none);
        if (is_error)
            return "";

        // BdUtils.log(" --- row:", TableUtils.conv_row_to_dc(row));
        // TableUtils.show_one_row(row);

        var _text = row[TranslatorManager.instance.language];
        // BdUtils.log(" --- _text:", _text);
        is_error = BdUtils.assert(_text, $" *** `code[{code}]`对应`language[{language}]`为空?", raise_error: false);
        if (is_error)
            return "";

        string translated_text = BdUtils.conv_to_string(_text);
        return translated_text;
    }

    [Header("csv文件路径")]
    public string csv_file_name = "translator.csv";

    DataTable _translator_dt;
    public DataTable translator_dt
    {
        get
        {
            if (_translator_dt == null)
            {
                BdUtils.log("--- reload _translator_dt");
                // string f_path = BdUtils.join(dir_path, file_name);
                // _translator_dt = TableUtils.read_csv(f_path, primary_key: "ID");

                _translator_dt = TableUtils.read_csv_data_on_all_platform(csv_file_name, primary_key: "code");

                TableUtils.show_data_table(_translator_dt, zh_len: 20);
            }
            return _translator_dt;
        }
    }


    void Start()
    {
        BdUtils.call(translator_dt);
    }

    void Update()
    {
        if (is_cn)
            language = "cn";
        else
            language = "en";

        if (use_translate)
            translate_all_text_comp();
    }

    public void translate_all_text_comp()
    {
        if (last_language == language)
            return;
        last_language = language;

        foreach (var kv in translate_comp_dc)
        {
            var value = kv.Value;
            value.translate_text();
        }
    }
}
